<template>
	<div class="sys-vislog-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label="t('system.log.vis.startTime')">
					<el-date-picker v-model="state.queryParams.beginTime" type="datetime" :placeholder="t('system.log.vis.startTime')" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
				</el-form-item>
				<el-form-item :label="t('system.log.vis.endTime')" prop="code">
					<el-date-picker v-model="state.queryParams.endTime" type="datetime" :placeholder="t('system.log.vis.endTime')" value-format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" />
				</el-form-item>
				<el-form-item :label="t('system.log.vis.name')">
					<el-input v-model="state.queryParams.name" :placeholder="t('system.log.vis.name')" clearable />
				</el-form-item> 
				<el-form-item :label="t('system.log.vis.status')">
					<el-select v-model="state.queryParams.success" :placeholder="t('system.log.vis.status')" clearable>
						<el-option :label="t('system.log.vis.success')" :value="1" />
						<el-option :label="t('system.log.vis.fail')" :value="0" />
					</el-select>
				</el-form-item>
				
				<el-form-item :label="t('system.log.vis.operator')">
					<el-input v-model="state.queryParams.account" :placeholder="t('system.log.vis.operator')" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'logvis:searchBtn'"> {{ t('system.btn.query') }} </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> {{ t('system.btn.reset') }} </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button icon="ele-DeleteFilled" type="danger" @click="clearLog" v-auth="'logvis:clearBtn'" disabled> {{ t('system.btn.clear') }} </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.logData" style="width: 100%" v-loading="state.loading" border>
				<el-table-column type="index" :label="t('system.common.index')" width="55" align="center" />
				<el-table-column prop="name" :label="t('system.log.vis.name')" width="150" align="center" show-overflow-tooltip />
				<el-table-column prop="ip" :label="t('system.log.vis.ip')" min-width="120" align="center" show-overflow-tooltip />
				<el-table-column prop="location" :label="t('system.log.vis.location')" min-width="150" align="center" show-overflow-tooltip />
				<el-table-column prop="browser" :label="t('system.log.vis.browser')" min-width="150" align="center" show-overflow-tooltip />
				<el-table-column prop="os" :label="t('system.log.vis.os')" width="120" align="center" show-overflow-tooltip />
				<el-table-column prop="success" :label="t('system.log.vis.status')" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.success === 1">{{ t('system.log.vis.success') }}</el-tag>
						<el-tag type="danger" v-else>{{ t('system.log.vis.fail') }}</el-tag>
					</template>
				</el-table-column> 
				<el-table-column prop="visType" :label="t('system.log.vis.visType')" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.visType === 1">{{ t('system.log.vis.logout') }}</el-tag>
						<el-tag v-else-if="scope.row.visType === 2">{{ t('system.log.vis.register') }}</el-tag>
						<el-tag v-else-if="scope.row.visType === 3">{{ t('system.log.vis.changePwd') }}</el-tag>
						<el-tag v-else-if="scope.row.visType === 4">{{ t('system.log.vis.thirdPartyAuth') }}</el-tag> 
						<el-tag v-else>{{ t('system.log.vis.login') }}</el-tag>
					</template>
				</el-table-column>

				<el-table-column prop="visTime" :label="t('system.log.vis.logTime')" width="160" align="center" fixed="right" show-overflow-tooltip />
				<el-table-column prop="account" :label="t('system.log.vis.account')" width="120" align="center" fixed="right" show-overflow-tooltip />
			</el-table>
			<el-pagination
				v-model:currentPage="state.tableParams.pageNo"
				v-model:page-size="state.tableParams.pageSize"
				:total="state.tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				size="small"
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>
		</el-card>
	</div>
</template>

<script lang="ts" setup name="sysVisLog">
import { onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

import { apiSysLogVisPagePost, apiSysLogVisClearPost } from '/@/api-services/api';

const state = reactive({
	loading: false,
	queryParams: {
		beginTime: undefined,
		endTime: undefined,
		success: undefined,
		name: undefined, 
		account: undefined, 
	},
	tableParams: {
		pageNo: 1,
		pageSize: 50,
		total: 0 as any,
	},
	logData: [] as Array<any>,
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	if (state.queryParams.beginTime == null) state.queryParams.beginTime = undefined;
	if (state.queryParams.endTime == null) state.queryParams.endTime = undefined;
	if (state.queryParams.success == null) state.queryParams.success = undefined;
	if (state.queryParams.name == null) state.queryParams.name = undefined;
	if (state.queryParams.account == null) state.queryParams.account = undefined;
	 

	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	var res = await apiSysLogVisPagePost(params);
	state.logData = res.data.data?.rows	 ?? [];
	state.tableParams.total = res.data.data?.totalRows;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.beginTime = undefined;
	state.queryParams.endTime = undefined;
	state.queryParams.success = undefined;
	state.queryParams.name = undefined;
	state.queryParams.account = undefined;
	handleQuery();
};

// 清空日志
const clearLog = async () => {
	state.loading = true;
	await apiSysLogVisClearPost();
	state.loading = false;

	ElMessage.success('清空成功');
	handleQuery();
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.pageNo = val;
	handleQuery();
};

const shortcuts = [
	{
		text: '今天',
		value: new Date(),
	},
	{
		text: '昨天',
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24);
			return date;
		},
	},
	{
		text: '上周',
		value: () => {
			const date = new Date();
			date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
			return date;
		},
	},
];
</script>
